export namespace TableTools {
    export function createKey(row: number, col: number) {
        return String(row) + ',' + String(col);
      }
    export function parseKey(key: string) {
        let arr = key.split(',');
        return { row: Number(arr[0]), col: Number(arr[1]) };
    }
    export function arrayToObj<T>(arr:Array<T>, ignoreValue: T|undefined = undefined): {[key:string]: T} {
        let res: {[key:string]: T} = {};
        for(let i = 0; i < arr.length; i++) {
            if(ignoreValue != undefined && arr[i] == ignoreValue) {
                continue;
            }
            res[i] = arr[i];
        }
        return res;
    }
    export function objToArray<T>(obj: {[key: string]: T}, defaultValue: T, maxLength: number):Array<T> {
        let arr: Array<T> = [];
        for(let i = 0; i < maxLength; i++) {
            if(obj.hasOwnProperty(String(i))) {
                arr.push(obj[String(i)]);
            }else{
                arr.push(defaultValue);
            }
        }
        return arr;
    }


    export function getDefaultSetting() {

    }

    export function getAllCellName(expressArr: string[]) {
        let res: string[] = [];
        for(let cell of expressArr) {
            if(/^[A-Z]{1,2}\d{1,3}$/i.test(cell)) {
                res.push(cell);
            }
        }
        return res;
    }
    /**
     * 
     * @param postion A1
     * @description 起始地址为0
     */
    export function transformExcelPosionToRowCol(postion: string) {
        if(/^[A-Z]{1}\d{1,3}$/i.test(postion)) {
           return {row: Number(postion.slice(1,postion.length)) - 1, col: postion.charCodeAt(0)-65};
        } else {
            let row = Number(postion.slice(2,postion.length));
            let col = (postion.charCodeAt(0) - 65) * 26 + (postion.charCodeAt(1) - 65)
            return {row: row, col: col};
        }
    }
    function convert(num: number){
        var result = [];
        while(num){
            var t = num % 26;
            if(!t){
                t = 26;
                -- num;
            }    
            result.push(String.fromCodePoint(t + 64));
            num = ~~(num / 26);
        }
        return result.reverse().join('');
    }
    export function transatRowColToExlPosition(position: {row: number, col: number}) {
        let fstChar = convert(position.col + 1);
        return fstChar + String(position.row+1);
    }
}